package com.zpark.mvc.config;

import com.zpark.interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.zpark.mvc.config
 * @ClassName: SpringMVCConfig
 * @Description:
 * @author: RZS
 * @date: 2024/3/11  9:09
 */
@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {


    /**
     * 配置预先配置了响应状态代码和/或视图的简单自动化控制器，以呈现响应主体。
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 添加对应路径响应状态码
        registry.addStatusController("/sc", HttpStatus.NOT_FOUND);
        // 添加默认访问路径  localhost:8080   localhost:8080/index.html
        registry.addViewController("/").setViewName("/main.html");
        registry.addViewController("/index.html").setViewName("/main.html");

    }

    /**
     * 拦截规则方法
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建自定义拦截器对象
        HandlerInterceptor userInterceptor = new UserInterceptor();
        // 注册拦截器
        InterceptorRegistration interceptor = registry.addInterceptor(userInterceptor);
        // 配置拦截规则
        List<String> blackPath = new ArrayList<>();
        blackPath.add("/**"); // 拦截所有请求路径

        interceptor.addPathPatterns(blackPath);

        // 配置白名单（不需要拦截的路径）
        List<String> whitePath = new ArrayList<>();
        whitePath.add("/main.html"); // 添加不需要拦截的路径

        interceptor.excludePathPatterns(whitePath);

    }
}
